package com.chainyoung.component_tmperson.ui.activity.debit;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.widget.TextView;

import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonActivityComponent;
import com.chainyoung.component_tmperson.di.module.PersonActivityModule;
import com.chainyoung.component_tmperson.entity.PreRepayment;
import com.chainyoung.component_tmperson.entity.WalletDebit;
import com.chainyoung.component_tmperson.ui.activity.debit.RepayDebitSuccessActivity;
import com.chainyoung.component_tmperson.ui.mvp.contract.ConfirmRepayContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.ConfirmRepayPresenter;

import java.math.BigDecimal;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;

public class ConfirmRepayDebitActivity extends BaseActivity<ConfirmRepayPresenter> implements ConfirmRepayContract.View {

    @BindView(R2.id.tv_repay_amount)
    TextView tvRepayAmount;
    @BindView(R2.id.txt_remained_repay)
    TextView txtRemainedRepay;
    @BindView(R2.id.tv_remained_repay_amount)
    TextView tvRemainedRepayAmount;
    @BindView(R2.id.txt_day_rate)
    TextView txtDayRate;
    @BindView(R2.id.tv_fee)
    TextView tvFee;
    @BindView(R2.id.txt_repay_method)
    TextView txtRepayMethod;
    @BindView(R2.id.tv_total_interest)
    TextView tvTotalInterest;
    @BindView(R2.id.txt_total)
    TextView txtTotal;
    @BindView(R2.id.tv_total_repay_amount)
    TextView tvTotalRepayAmount;
    @BindView(R2.id.txt_repay_account)
    TextView txtRepayAccount;
    @BindView(R2.id.tv_repay_account)
    TextView tvRepayAccount;
    @BindView(R2.id.tv_confirm_repay)
    TextView tvConfirmRepay;


    private String debitId;
    private WalletDebit walletDebit;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPersonActivityComponent.builder()
                .appComponent(appComponent)
                .personActivityModule(new PersonActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUI() {
        setCurrentTitle("还款明细");
        debitId = getIntent().getStringExtra("debitId");
        walletDebit = (WalletDebit) getIntent().getSerializableExtra("walletDebit");

    }

    @Override
    protected void initData() {
        getPreRepayInfo();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_activity_confirm_repay;
    }

    private void updateView(PreRepayment preRepayment) {
        tvRepayAmount.setText(StringUtil.subZeroAndDot(preRepayment.getUsed_principal()));
        tvRemainedRepayAmount.setText(StringUtil.subZeroAndDot(preRepayment.getUsed_principal()));
        tvFee.setText(StringUtil.subZeroAndDot(preRepayment.getPrepayment_fee_amount()));
        tvTotalInterest.setText(StringUtil.subZeroAndDot(preRepayment.getInterests()));

        BigDecimal bgTotal = new BigDecimal(preRepayment.getUsed_principal()).add(new BigDecimal(preRepayment.getInterests())).add(
                new BigDecimal(preRepayment.getPrepayment_fee_amount()));
        tvTotalRepayAmount.setText(StringUtil.subZeroAndDot(String.valueOf(bgTotal)));

    }


    @OnClick(R2.id.tv_confirm_repay)
    void confirmRepay() {
        AlertDialog.Builder builder2 = new AlertDialog.Builder(mContext);
        builder2.setMessage(R.string.tmperson_confirm_pre_reapay)
                .setPositiveButton(mContext.getString(R.string.confirm), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        confirmPreRepay();
                        dialog.dismiss();
                    }
                })
                .setNegativeButton(mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        builder2.create();
        builder2.show();
    }

    private void confirmPreRepay() {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put(LibConstant.TOKEN, SPUtil.getString(LibConstant.TOKEN));
        map.put("loans_id", debitId);
        mPresenter.confirmRepay(map);
    }


    private void getPreRepayInfo() {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put(LibConstant.TOKEN, SPUtil.getString(LibConstant.TOKEN));
        map.put("loans_id", debitId);
        mPresenter.getPreRepaymentInfo(map);
    }


    @Override
    public void onGetPreRepaymentInfo(PreRepayment data) {
        updateView(data);
    }

    @Override
    public void onGetPreRepaymentFailed() {

    }

    @Override
    public void onConfirmRepaySuccess(String data) {
        Intent intent=new Intent(mContext,RepayDebitSuccessActivity.class);
        intent.putExtra("walletDebit", walletDebit);
        mContext.startActivity(intent);
        finish();
    }


}
